package com.hmall.config;

import com.hmall.common.BaseContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String authorization = request.getHeader("authorization");

        if (StringUtils.isBlank(authorization)) {
            throw new RuntimeException("WARNING:[USER_NOT_LOGGED_IN]");
        }
        Long id = Long.valueOf(authorization);
        BaseContext.setCurrentId(id);
        return true;
    }
}
